package com.test.day02consumer;

/**
 * @program: javaTest
 * @author: luhd
 * @create: 2021-07-31
 */
public class BaoZiPu extends Thread{
    private BaoZi bz;

    public BaoZiPu(BaoZi bz) {
        this.bz = bz;
    }
    // 设置线程任务(run)；生产包子
    @Override
    public void run() {
        int count = 0;
        while(true){
            synchronized (bz){
                if(bz.flag == true){
                    try {
                        bz.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 唤醒之后执行，包子铺生产包子
                if(count%2==0){
                    bz.pi = "薄皮";
                    bz.xian = "三鲜馅";
                }else{
                    bz.pi = "冰皮";
                    bz.xian = "；牛肉大葱馅";
                }
                count ++;
                System.out.println("包子铺正在生产:"+bz.pi+bz.xian+"包子");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                bz.flag = true;
                bz.notify();
                System.out.println("包子铺已经生产好了:"+bz.pi+bz.xian+"包子,你可以开始吃了!");
            }
        }
    }
}
